package com.kws.merchant.service.impl;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.kws.common.security.auth.AuthUtil;
import com.kws.merchant.domain.OperationMaintain;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.merchant.domain.po.MaintainPo;
import com.kws.merchant.mapper.OperationMaintainMapper;
import com.kws.merchant.service.OperationMaintainService;
import com.kws.merchant.util.FileUtil;
import com.kws.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 运维-报修，抢修管理(OperationMaintain)表服务实现类
 *
 * @author yxy
 * @since 2024-10-28 14:36:38
 */
@Service("operationMaintainService")
public class OperationMaintainServiceImpl extends ServiceImpl<OperationMaintainMapper, OperationMaintain> implements OperationMaintainService {

    @Resource
    private OperationMaintainMapper operationMaintainMapper;

    /**
     * 分页查询列表
     */
    @Override
    public Page<OperationMaintain> pageList(MaintainPo maintainPo) {
        Integer pageSize = maintainPo.getPageSize();
        Integer totalPage = maintainPo.getTotalPage();
        String faultName = maintainPo.getFaultName();//故障名称
        String faultType = maintainPo.getFaultType();//故障类型
        String type = maintainPo.getType();//类型0保修 1抢修
        String emergencyDegree = maintainPo.getEmergencyDegree();//紧急程度0低 1中 2高
        SysUser sysUser = AuthUtil.getLoginUser().getSysUser();
        String sql=" ";
        if (sysUser.getCompanyId() != null) {
            sql = " and o.merchant_id=" + sysUser.getCompanyId();
        }
        if (StringUtils.isNotEmpty(faultName)) {
            sql += " and o.fault_name='" + faultName+"'";
        }
        if (StringUtils.isNotEmpty(faultType)) {
            sql += " and o.fault_type=" + faultType;
        }
        if (StringUtils.isNotEmpty(type)) {
            sql += " and o.type=" + type;
        }
        if (StringUtils.isNotEmpty(emergencyDegree)) {
            sql += " and o.emergency_degree=" + emergencyDegree;
        }
        int sizeNum = (pageSize - 1) * totalPage;//计算分页
        int total = operationMaintainMapper.countToTal(sql);
        //查询签名数据
        List<OperationMaintain> records = operationMaintainMapper.getRecords(sql, sizeNum, totalPage);
        records.forEach(x->{
            if (x.getFile()!=null){
            x.setFile(FileUtil.fixFileName()+x.getFile());
            }
        });
        Page<OperationMaintain> page = new Page<>();
        page.setRecords(records);
        page.setTotal(total);
        page.setCurrent(pageSize);
        page.setPages((total + totalPage - 1) / totalPage);
        page.setSize(totalPage);
        return page;
    }

    /**
     * 报修-抢修处理
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object disposeMaintain(OperationMaintain operationMaintain) {
        if (operationMaintain.getStatus()!=2){
            throw new RuntimeException("工单不是待处理状态，无法处理！！");
        }
        operationMaintain.setStatus(1);//修改状态为待完成
        operationMaintain.setFile(FileUtil.subFileName(operationMaintain.getFile()));
        return this.updateById(operationMaintain);
    }

    /**
     * 完成维修
     *
     * @param id
     * @return
     */
    @Override
    public Object fulfilTask(Long id) {
        OperationMaintain maintain = this.getById(id);
        if (ObjectUtils.isEmpty(maintain)) {
            throw new RuntimeException("无法查询到相关信息");
        }
        if (maintain.getStatus()!=1){
            throw new RuntimeException("工单不是待完成状态，无法处理！！");
        }
        maintain.setStatus(0);//修改状态为已完成
        return this.updateById(maintain);
    }

    /**
     * 查看
     *
     * @param id
     * @return
     */
    @Override
    public OperationMaintain detailById(Long id) {
        OperationMaintain maintain = operationMaintainMapper.detailById(id);
        if (ObjectUtils.isEmpty(maintain)) {
            throw new RuntimeException("无法查询到相关信息");
        }
        if (maintain.getFile()!=null){
        maintain.setFile(FileUtil.fixFileName()+maintain.getFile());
        }
        return maintain;
    }

    @Override
    public Double countWorkTypeBySql(Long companyId, int type, String sql) {
        return operationMaintainMapper.countWorkTypeBySql(companyId,type,sql);
    }


}
